<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
	
	case 'addPaper' :
		addPaper();
		break;
		
	case 'modifyPaper' :
		modifyPaper();
		break;
		
	case 'deletePaper' :
		deletePaper();
		break;
	
	case 'deleteImage' :
		deleteImage();
		break;
    

	default :
	    // if action is not defined or unknown
		// move to main Paper page
		header('Location: index.php');
}


function addPaper()
{
    $values 	= $_POST['tg'];
    
	$khoi_id	 = $_POST['cboStyle'];
	$mabb        = $_POST['txtMabb'];
	$tenbb		 = $_POST['txtTieude'];
	$tomtat_bb 	 = $_POST['mtxTomtat'];
	
	$tc_id 		 = $_POST['cboTapchi'];
	$cttc_id	= $_POST['cboNumber'];
	$loaitc_id = $_POST['cboJournalStyle'];
	
	
	$ngaydang	 = $_POST['txtNgaydang'];
	$arr = explode("-", $ngaydang);
	$ngaydang = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
	
	$tutrang	 = $_POST['txtTutrang'];
	$dentrang	 = $_POST['txtDentrang'];
	
	$pathFile = array(); $count = 0;
	for ( $i = 0; $i < 10; $i++){
		if (isset($_FILES['uploadFiles']['name'][$i]) && (!empty($_FILES['uploadFiles']['name'][$i]))){
			$pathFile[$i] = "../../data/".$_FILES['uploadFiles']['name'][$i];
			copy($_FILES['uploadFiles']['tmp_name'][$i], $pathFile[$i]);
			$count ++;
		}
	}
	$fileContent = '';
	for ( $i = 0; $i < $count; $i++){
		if ($fileContent =='') {
			$fileContent = $_FILES['uploadFiles']['name'][$i];
		}
		else {
			$fileContent = $fileContent.','.$_FILES['uploadFiles']['name'][$i];
		}
	}
	//echo $fileContent; 
	//exit();

	$sql1   = "INSERT INTO baibao (mabb, tenbb, tomtat_bb, ngaydang, khoi_id, tutrang, dentrang, noidung_bb, cttc_id)
	          VALUES ('$mabb', '$tenbb', '$tomtat_bb', '$ngaydang', $khoi_id, $tutrang, $dentrang,'$fileContent', $cttc_id)";			  		  
	//echo $sql1; exit();
	dbQuery($sql1);
	
	$values = str_replace('{bb_id}', mysql_insert_id(), $values);
	$sql3   = "INSERT INTO tacgia_baibao(tg_id, bb_id, thutu)
	          VALUES $values ";
	echo $sql3;
	dbQuery($sql3);
		
	header("Location: index.php?khoi_id=$khoi_id");
}

	//Modify a Paper

function modifyPaper()
{
	$tg_add = $_POST['tg'];
	$tg_sel = $_POST['tg_sel'];
	$strNoidung = $_POST['noidung_bb'];
	$id = (int)$_GET['id'];
	
	$khoi_id      = (int)$_POST['cboStyle'];
    $mabb        = $_POST['txtMabb'];
	$tenbb		 = $_POST['txtTieude'];
	$tomtat_bb 	 = $_POST['mxtTomtat'];
	
	$tc_id 		 = $_POST['cboTapchi'];
	$cttc_id	= $_POST['cboNumber']; 
	
	$ngaydang    = $_POST['txtNgaydang'];
	
	$arr = explode("-", $ngaydang);
	$ngaydang = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
	
	//$noidung_bb	 = $_POST['mtxNoidung'];
	//$tc_id 		=  = $_POST['cboTapchi'];
	$tutrang	 = $_POST['txtTutrang'];
	$dentrang	 = $_POST['txtDentrang'];

	// upload files if any
	$pathFile = array(); $count = 0;
	for ( $i = 0; $i < 10; $i++){
		if (isset($_FILES['uploadFiles']['name'][$i]) && (!empty($_FILES['uploadFiles']['name'][$i]))){
			$pathFile[$i] = "../../data/".$_FILES['uploadFiles']['name'][$i];
			copy($_FILES['uploadFiles']['tmp_name'][$i], $pathFile[$i]);
			$count ++;
		}
	}
	$fileContent = '';
	for ( $i = 0; $i < $count; $i++){
		if ($fileContent =='') {
			$fileContent = $_FILES['uploadFiles']['name'][$i];
		}
		else {
			$fileContent = $fileContent.','.$_FILES['uploadFiles']['name'][$i];
		}
	}
	// merge content names
	if (isset($strNoidung) && !empty($strNoidung))
	{
		if (!empty($fileContent)) {
			$fileContent = ',' . $fileContent;
		}
		$fileContent = $strNoidung . $fileContent;
	}
	
	$sql1   = "UPDATE baibao 
               SET mabb = '$mabb', tenbb = '$tenbb', tomtat_bb = '$tomtat_bb', ngaydang = '$ngaydang', khoi_id = $khoi_id, tutrang = $tutrang, dentrang = $dentrang, 
			   noidung_bb = '$fileContent', cttc_id = $cttc_id
			   WHERE id = $id" ;
			 //echo $sql1; exit();		   
	$result = dbQuery($sql1);
	
	if (isset($tg_add) && $tg_add != '') {
		
	$insertValues = str_replace('{bb_id}', $id, $tg_add);
	
	if ($insertValues != "") {
		$sql3   = "INSERT INTO tacgia_baibao(tg_id, bb_id, thutu)
					  VALUES $insertValues ";
			//echo $sql3; exit();
		dbQuery($sql3);
	}
	}
	if (isset($tg_sel) && !empty($tg_sel)) {
		// xoa tat ca tac gia neu no khong thuoc danh sach cac tac gia da duoc chon
		$sql_delete = "DELETE FROM tacgia_baibao WHERE bb_id = $id AND tg_id NOT IN " . $tg_sel;
		dbQuery($sql_delete);
	}
	else
	{
		$sql_delete = "DELETE FROM tacgia_baibao WHERE bb_id = $id ";
		dbQuery($sql_delete);
	}
	header('Location: index.php');			  	  
}

/*
	Remove a Paper
*/
function deletePaper()
{
	if (isset($_GET['id']) && $_GET['id'] > 0) {
	$id = (int)$_GET['id'];
	} else {
		header('Location:index.php');
	}	
	$sql1 = "UPDATE baibao SET hieu_luc = 0 WHERE id = $id"; 
	
	dbQuery($sql1);
		
    header('Location: index.php'); 
}
?>